package com.example.egweibo1.base.filters;

import com.example.egweibo1.pojo.vo.UserVo;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebFilter(filterName = "LoginCheckFilter", urlPatterns = {"/v1/comment/*"
//        ,"/v1/weibo/*"
})
@Slf4j
public class LoginCheckFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        log.debug("过滤器");
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo) session.getAttribute("user");
        if (userVo != null) {
            filterChain.doFilter(request, response);

        } else
            //重定向
            response.sendRedirect("/login.html");
    }
}
